package mouseEvents;

import graphic.MyFrame;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import Graph.Graph;
import Graph.optimalWay;
import Graph.pathway;

public class DrawFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static DrawLines DrLine1 ;

	/**
	 * @throws FileNotFoundException 
	 * 
	 */

	public DrawFrame( final Graph city,final int first,final int finish) throws FileNotFoundException {
        super("Painting Graph");
        
        JMenuBar menuBar = new JMenuBar();
		menuBar.add(createFileMenu());
		
		menuBar.add(new JMenu("file"));
		menuBar.add(new JMenu("edit"));
		menuBar.add(Box.createHorizontalGlue());
		JLabel icon = new JLabel(new ImageIcon("images/download.gif"));
		icon.setBorder(BorderFactory.createLoweredBevelBorder());
		menuBar.add(icon);
        
		//setJMenuBar(menuBar);
        
        final JPanel cp = new JPanel(new BorderLayout(5, 5));
        cp.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        final JPanel draw = new JPanel(new BorderLayout(5, 5));
        draw.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        final PointPanel Pinp = new PointPanel();

        Pinp.setBorder(BorderFactory.createLineBorder(Color.black));
        cp.add(new JScrollPane(Pinp), BorderLayout.CENTER);
        JButton btn = new JButton("Remove only one");
        btn.setOpaque(false);
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Pinp.remove();
            }
        });
        cp.add(btn, BorderLayout.NORTH);
        JButton line = new JButton("Draw Lines");
        line.setOpaque(true);
        line.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //Pinp.remove();
    	        final DrawLines DrLine = new DrawLines(Pinp);
    	        DrLine1 = DrLine;
    	        
            	
            	cp.setVisible(false);
            	cp.remove(Pinp);
            	draw.add(new JScrollPane(DrLine),BorderLayout.CENTER);
            	
            	
            	setContentPane(draw);
            	
            	draw.setVisible(true);
            }
        });
        cp.add(line, BorderLayout.WEST);
        JButton end = new JButton("End Graph and write in file");
        end.setOpaque(false);
        end.addActionListener(new ActionListener() {
            @SuppressWarnings("static-access")
			public void actionPerformed(ActionEvent e) {
            Point[] point1;
            
            city.setPoint(DrLine1.generatePoints());
            point1 = DrLine1.generatePoints();
            city.setNumber(DrLine1.sizeP());
            Point[] point2 = city.getPoint();
            city.setGraph(DrLine1.generateIncedent());
            try {
				city.writeFile(city.getGraph());
			} catch (IOException e2) {
				e2.printStackTrace();
			}
               
               
               
            JFrame graph = null;
            city.setNumber(DrLine1.sizeP());
          	city.Make_Matrix();
			optimalWay routeMap = new optimalWay (city);
			pathway Way = new pathway();
			routeMap.setCourse (city, Way, first, finish, 0);
			try {
				graph = new MyFrame(city);
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			}
		
       		graph.setVisible(false);
       		
       		Graph City = null;
			
				try {
					City = new Graph("Point_of_Graph", "Incedent");
				} catch (FileNotFoundException e2) {
					e2.printStackTrace();
				}
			optimalWay routeMap1 = new optimalWay (City);
    		pathway Way1 = new pathway();
    		routeMap1.setCourse (City, Way1, first, finish, 0);
    		try {
				JFrame graph1 = new MyFrame(City);
				graph1.setVisible(true);
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			}
       		
       		
            }
        });
       draw.add(end, BorderLayout.SOUTH);
        

        final JCheckBox cb = new JCheckBox("Clear All");
        cb.setSelected(false);
        cb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Pinp.setUnexpectedRepaints(cb.isSelected());
            }
        });
        cp.add(cb, BorderLayout.SOUTH);
        setContentPane(cp);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(1000, 700);
        setLocationRelativeTo(null);
    }

	
	
	private JMenu createFileMenu() {
		JMenu file = new JMenu("file");
		JMenuItem open = new JMenuItem("open", new ImageIcon("images/open16.gif"));
		JMenuItem exit = new JMenuItem(new ExitAction());
		file.addSeparator();
		file.add(exit);
		return file;
	}
	class ExitAction extends AbstractAction {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		ExitAction() {
			putValue(NAME, "exit");
		}
		
		
		
		public void actionPerformed(ActionEvent e) {
		System.exit(0);
		}
	}
	
	public static void moreMany(){
		JFrame myframe = null;
		JOptionPane.showMessageDialog(
				myframe, "Max Vertexes Reached");
	}
}	
